package com.bbcare.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.bbcare.comm.ToolUtil;

/**
 * @Description: TODO
 * @author MLi
 * @date 2018年4月9日
 * @version V1.0
 */
public class CheckInputRootInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            CheckInputRoot checkInputRoot = handlerMethod.getMethodAnnotation(CheckInputRoot.class);
            if (checkInputRoot != null) {
                //String body = ToolUtil.getRequestBody(request);
                String body = new String(((WrappedHttpServletRequest) request).getBytes(), "UTF-8");
                ToolUtil.checkInputRoot(body);
            }
        }
        return super.preHandle(request, response, handler);
    }

}
